package com.emapp.lccode.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.emapp.lccode.R;
import com.emapp.lccode.base.BaseApplication;
import com.emapp.lccode.base.BaseConfig;
import com.emapp.lccode.base.UmInitConfig;
import com.emapp.lccode.databinding.PopPrivacyBinding;
import com.emapp.lccode.utils.MmkvUtil;
import com.umeng.commonsdk.UMConfigure;

/**
 * Author:YanshanZhang
 * E-mail:690810892@qq.com
 * Date: 2022/7/9 9:42
 * Desc:
 */
public class PrivacyDialog extends DialogFragment {
    PopPrivacyBinding binding;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog == null || dialog.getWindow() == null) {
            return;
        }
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = PopPrivacyBinding.inflate(inflater, container, false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return binding.getRoot();
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().getWindow().setWindowAnimations(R.style.dialog_style);
        binding.reminderMessage.setMovementMethod(LinkMovementMethod.getInstance());
        binding.reminderMessage.setText(getStr());
        binding.cancelDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConfirmListener listener = (ConfirmListener) getActivity();
                if (listener == null) {
                    return;
                }
                listener.onCancel();
                dismiss();
            }
        });
        binding.confirmDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//
                MmkvUtil.putBoolean(BaseConfig.CANCEL_PRIVACY_SHOW,true);
                UMConfigure.submitPolicyGrantResult(BaseApplication.instance, true);
                UmInitConfig umInitConfig = new UmInitConfig();
                umInitConfig.UMinit(BaseApplication.instance);
//                //QQ官方sdk授权
//                Tencent.setIsPermissionGranted(true);
//                //推送平台多维度推送决策必须调用方法(需要同意隐私协议之后初始化完成调用)
//                PushAgent.getInstance(SplashTestActivity.this).onAppStart();

                ConfirmListener listener = (ConfirmListener) getActivity();
                if (listener == null) {
                    return;
                }
                listener.onConfirm();
                dismiss();
            }
        });
    }

    public static final boolean isNull(String str) {
        return str == null || "".equals(str.trim());
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//        // Get the layout inflater
//        LayoutInflater inflater = getActivity().getLayoutInflater();
//        View view = inflater.inflate(R.layout.fragment_login_dialog, null);
//        mUsername = (EditText) view.findViewById(R.id.id_txt_username);
//        mPassword = (EditText) view.findViewById(R.id.id_txt_password);
//        // Inflate and set the layout for the dialog
//        // Pass null as the parent view because its going in the dialog layout
//        builder.setView(view)
//                // Add action buttons
//                .setPositiveButton("Sign in",
//                        new DialogInterface.OnClickListener()
//                        {
//                            @Override
//                            public void onClick(DialogInterface dialog, int id)
//                            {
//                                LoginInputListener listener = (LoginInputListener) getActivity();
//                                listener.onLoginInputComplete(mUsername
//                                        .getText().toString(), mPassword
//                                        .getText().toString());
//                            }
//                        }).setNegativeButton("Cancel", null);
//        return builder.create();
//    }
        return super.onCreateDialog(savedInstanceState);
    }

    public interface ConfirmListener {
        void onConfirm();
        void onCancel();
    }
    @NonNull
    private SpannableStringBuilder getStr() {
        String start = "在您使用创意农场APP服务前, 请认真阅读";
        String str = start + "《用户协议》、《隐私政策》等全部条款, 在您同意并接受全部条款后再开始使用我们的服务。";
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(str);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
//                postSticky(null, Constant.XITYI, "用户协议");
//                goTo(AppPrivacyActivity.class);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE);//设置颜色
                ds.setUnderlineText(false);//去掉下划线
            }
        }, start.length(), start.length() + 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
//                postSticky(null, Constant.XITYI, "隐私政策");
//                goTo(AppSystemWebActivity.class);
                //goTo(AppProtocolActivity.class);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE);//设置颜色
                ds.setUnderlineText(false);//去掉下划线
            }
        }, start.length() + 7, start.length() + 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return stringBuilder;
    }
}
